//
//  OwnImageViewController.m
//  Puzzle_Crazy
//
//  Created by VIETHUNG on 4/8/14.
//  Copyright (c) 2014 DanielLe. All rights reserved.
//

#import "OwnImageViewController.h"
#import "MyCell.h"
#import "PuzzleObject.h"
#import "UIButton+Block.h"
#import "PuzzleViewController.h"
#import "CellObject.h"
#import "DBManager.h"
#import "IBActionSheet.h"
#import "PECropViewController.h"

@interface OwnImageViewController ()

@end

@implementation OwnImageViewController
@synthesize arrData;
@synthesize idPu;
@synthesize imgPicker;

- (void)viewDidLoad
{
    [super viewDidLoad];
    arrData = [[NSMutableArray alloc] init];
    
    // Remove table cell separator
    [myTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    // Assign our own backgroud for the view
    myTableView.backgroundColor = [UIColor clearColor];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"common_bg"]];
    
    // Add padding to the top of the table view
    UIEdgeInsets inset = UIEdgeInsetsMake(5, 0, 0, 0);
    myTableView.contentInset = inset;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.imgPicker = [[UIImagePickerController alloc] init];
    self.imgPicker.delegate = self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - DataSource UITableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  arrData.count;
}

- (UIImage *)cellBackgroundForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger rowCount = [self tableView:myTableView numberOfRowsInSection:0];
    NSInteger rowIndex = indexPath.row;
    UIImage *background = nil;
    
    if (rowIndex == 0) {
        background = [UIImage imageNamed:@"cell_top.png"];
    } else if (rowIndex == rowCount - 1) {
        background = [UIImage imageNamed:@"cell_bottom.png"];
    } else {
        background = [UIImage imageNamed:@"cell_middle.png"];
    }
    
    return background;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    PuzzleObject *puzzleObj = (PuzzleObject*)[arrData objectAtIndex:indexPath.row];
    cell.imgViewProduct.image = [UIImage imageNamed:puzzleObj.Objimage];
    
    [cell.btnMedium setEnabled:NO];
    [cell.btnHard setEnabled:NO];
    /* star easy */
    if ([puzzleObj.Objeasy intValue] >= k1StarEasy) {
        // 1 star
        cell.starEasy1.image = [UIImage imageNamed:@"star2.png"];
        
    } else if (k2StarEasy < [puzzleObj.Objeasy intValue] && [puzzleObj.Objeasy intValue] < k1StarEasy) {
        // 2 star
        cell.starEasy1.image = [UIImage imageNamed:@"star2.png"];
        cell.starEasy2.image = [UIImage imageNamed:@"star2.png"];
        
    } else if (k3StarEasy < [puzzleObj.Objeasy intValue] && [puzzleObj.Objeasy intValue] < k2StarEasy) {
        // 3 star
        cell.starEasy1.image = [UIImage imageNamed:@"star2.png"];
        cell.starEasy2.image = [UIImage imageNamed:@"star2.png"];
        cell.starEasy3.image = [UIImage imageNamed:@"star2.png"];
        [cell.btnEasy setEnabled:NO];
        [cell.btnMedium setEnabled:YES];
    }
    
    /* star medium */
    if ([puzzleObj.Objmedium intValue] >= k1StarEasy) {
        // 1 star
        cell.starMedium1.image = [UIImage imageNamed:@"star2.png"];
        
    } else if (k2StarEasy < [puzzleObj.Objmedium intValue] && [puzzleObj.Objmedium intValue] < k1StarEasy) {
        // 2 star
        cell.starMedium1.image = [UIImage imageNamed:@"star2.png"];
        cell.starMedium2.image = [UIImage imageNamed:@"star2.png"];
        
    } else if (k3StarEasy < [puzzleObj.Objmedium intValue] && [puzzleObj.Objmedium intValue] < k2StarEasy) {
        // 3 star
        cell.starMedium1.image = [UIImage imageNamed:@"star2.png"];
        cell.starMedium2.image = [UIImage imageNamed:@"star2.png"];
        cell.starMedium3.image = [UIImage imageNamed:@"star2.png"];
        [cell.btnMedium setEnabled:NO];
        [cell.btnHard setEnabled:YES];
    }
    
    /* star hard */
    if ([puzzleObj.Objhard intValue] >= k1StarEasy) {
        // 1 star
        cell.starHard1.image = [UIImage imageNamed:@"star2.png"];
        
    } else if (k2StarEasy < [puzzleObj.Objhard intValue] && [puzzleObj.Objhard intValue] < k1StarEasy) {
        // 2 star
        cell.starHard1.image = [UIImage imageNamed:@"star2.png"];
        cell.starHard2.image = [UIImage imageNamed:@"star2.png"];
        
    } else if (k3StarEasy < [puzzleObj.Objhard intValue] && [puzzleObj.Objhard intValue] < k2StarEasy) {
        // 3 star
        cell.starHard1.image = [UIImage imageNamed:@"star2.png"];
        cell.starHard2.image = [UIImage imageNamed:@"star2.png"];
        cell.starHard3.image = [UIImage imageNamed:@"star2.png"];
        [cell.btnHard setEnabled:NO];
    }
    
    [cell.btnEasy addAction:^(UIButton *btn) {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
        PuzzleViewController *puzzleView = (PuzzleViewController*)[storyboard instantiateViewControllerWithIdentifier:@"PuzzleViewController"];
        puzzleView.level = levelEasy;
        puzzleView.strImage = puzzleObj.Objimage;
        [puzzleView setModalPresentationStyle:UIModalPresentationCustom];
        puzzleView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [self presentViewController:puzzleView animated:YES completion:nil];
        
    } forControlEvents:UIControlEventTouchUpInside];
    
    [cell.btnMedium addAction:^(UIButton *btn) {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
        PuzzleViewController *puzzleView = (PuzzleViewController*)[storyboard instantiateViewControllerWithIdentifier:@"PuzzleViewController"];
        puzzleView.level = levelMedium;
        puzzleView.strImage = puzzleObj.Objimage;
        [puzzleView setModalPresentationStyle:UIModalPresentationCustom];
        puzzleView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [self presentViewController:puzzleView animated:YES completion:nil];
        
    } forControlEvents:UIControlEventTouchUpInside];
    
    [cell.btnHard addAction:^(UIButton *btn) {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
        PuzzleViewController *puzzleView = (PuzzleViewController*)[storyboard instantiateViewControllerWithIdentifier:@"PuzzleViewController"];
        puzzleView.level = levelHard;
        puzzleView.strImage = puzzleObj.Objimage;
        [puzzleView setModalPresentationStyle:UIModalPresentationCustom];
        puzzleView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [self presentViewController:puzzleView animated:YES completion:nil];
        
    } forControlEvents:UIControlEventTouchUpInside];
    
    
    // Asign our own background image for the cell
    UIImage *background = [self cellBackgroundForRowAtIndexPath:indexPath];
    UIImageView *cellBackgroundView = [[UIImageView alloc] initWithImage:background];
    cellBackgroundView.image = background;
    cell.backgroundView = cellBackgroundView;
    
    return cell;
}

#pragma mark - Delegate UITableView
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

- (IBAction)back:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)addImage:(UIButton *)sender {
    NSString *other1 = @"Album";
    NSString *other2 = @"Camera";
    NSString *other3 = @"Facebook";
    NSString *cancelTitle = @"Cancel";
    IBActionSheet *actionSheet = [[IBActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:cancelTitle destructiveButtonTitle:nil otherButtonTitlesArray:@[other1, other2, other3]];
    [actionSheet showInView:self.view];
}

#pragma mark - UIActionSheet Delegate
- (void)actionSheet:(IBActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    switch (buttonIndex) {
        case 0:
            // album
            self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentViewController:self.imgPicker animated:NO completion:nil];
            break;
            
        case 1:
            // camera
            self.imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self presentViewController:self.imgPicker animated:YES completion:nil];
            break;
            
        case 2:
            // facebook
            break;
            
        case 3:
            // cancel
            break;
    }
}

#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo
{
    NSLog(@"%@",img);
    [self dismissViewControllerAnimated:YES completion:nil];
    [self openEditor:img];
}

- (void)openEditor:(UIImage *)image
{
//    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
//    PECropViewController *controller = (PECropViewController*)[storyboard instantiateViewControllerWithIdentifier:kPuzzleIP5];
    PECropViewController *controller = [[PECropViewController alloc] init];
//    controller.delegate = self;
    controller.image = image;
    
    UIImage *_image = image;
    CGFloat width = _image.size.width;
    CGFloat height = _image.size.height;
    CGFloat length = MIN(width, height);
    controller.imageCropRect = CGRectMake((width - length) / 2,
                                          (height - length) / 2,
                                          length,
                                          length);
    
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:controller];
    
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
    }
    
    [self presentViewController:controller animated:YES completion:NULL];
}

@end
